package main

import (
	"os/exec"
	"fmt"
	"context"
	"time"
)

type result struct {
	err error
	output []byte
}

func main() {
	resultChan := make(chan *result, 5)
	ctx, cancelFunc := context.WithCancel(context.TODO())
	go func() {
		cmd := exec.CommandContext(ctx,"/bin/bash", "-c", "sleep 2;echo hello")
		output, err := 	cmd.CombinedOutput()
		if err != nil {
			println(err)
		}

		resultChan <- &result{
			err: err,
			output:output,
		}
	}()

	time.Sleep(1 * time.Second)

	cancelFunc()

	res := <- resultChan

	fmt.Println(res.err, string(res.output))
}
